package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallGoods;
import com.tencreat.domain.mall.MallMaterial;
import com.tencreat.domain.mall.dto.MallConsultRequDTO;
import com.tencreat.service.mall.IMallMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 朋友圈素材Controller
 *
 * @author tencreat
 * @date 2022-01-10
 */
@Api(tags="朋友圈素材api")
//@RestController
@RequestMapping("/system/material")
public class AdminMallMaterialController extends BaseController {
   // @Autowired
    private IMallMaterialService mallMaterialService;

    /**
     * 查询朋友圈素材列表
     */
    @ApiOperation(value="查询朋友圈素材列表")
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallMaterial>> list(@RequestBody MallConsultRequDTO mallMaterialVo) {
        LambdaQueryWrapper<MallMaterial> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(mallMaterialVo.getTitle())){
            wrapper.like(MallMaterial::getTitle, mallMaterialVo.getTitle());
        }
        if(mallMaterialVo.getStartTime() != null){
            wrapper.gt(MallMaterial::getCreateTime, mallMaterialVo.getStartTime().toLocalDate());
            wrapper.lt(MallMaterial::getCreateTime, mallMaterialVo.getEndTime().toLocalDate());
        }
        wrapper.eq(mallMaterialVo.getState() != null, MallMaterial::getState,mallMaterialVo.getState());
        wrapper.orderByDesc(MallMaterial::getTop).orderByAsc(MallMaterial::getSort).orderByDesc(MallMaterial::getCreateTime);
        Page<MallMaterial> page = new Page<>(mallMaterialVo.getPageNum(), mallMaterialVo.getPageSize());
        return HttpResult.ok(mallMaterialService.page(page, wrapper));
    }

    /**
     * 导出朋友圈素材列表
     */
    @ApiOperation(value="导出朋友圈素材列表")
    @PreAuthorize("@ss.hasPermi('system:material:export')")
    @Log(title = "商城管理>朋友圈素材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallMaterial mallMaterial) {
        List<MallMaterial> list = mallMaterialService.selectMallMaterialList(mallMaterial);
        ExcelUtil<MallMaterial> util = new ExcelUtil<MallMaterial>(MallMaterial.class);
        return util.exportExcel(list, "material");
    }

    /**
     * 获取朋友圈素材详细信息
     */
    @ApiOperation(value="获取朋友圈素材详细信息")
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallMaterial> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallMaterialService.selectMallMaterialById(id));
    }

    /**
     * 新增朋友圈素材
     */
    @ApiOperation(value="新增朋友圈素材")
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @Log(title = "商城管理>朋友圈素材", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallMaterial mallMaterial) {
        return to(mallMaterialService.insertMallMaterial(mallMaterial));
    }

    /**
     * 修改朋友圈素材
     */
    @ApiOperation(value="修改朋友圈素材")
    @PreAuthorize("@ss.hasPermi('system:material:edit')")
    @Log(title = "商城管理>朋友圈素材", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallMaterial mallMaterial) {
        return to(mallMaterialService.updateMallMaterial(mallMaterial));
    }

    /**
     * 删除朋友圈素材
     */
    @ApiOperation(value="删除朋友圈素材")
    @PreAuthorize("@ss.hasPermi('system:material:remove')")
    @Log(title = "商城管理>朋友圈素材", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallMaterialService.deleteMallMaterialByIds(ids));
    }

    /*****
     *  将当前商品的排序减1 顺序上升一位
     * @param mallMaterial
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Boolean> reSortOrderReduce(@RequestBody MallMaterial mallMaterial){
        return HttpResult.ok(mallMaterialService.reSortOrderReduce(mallMaterial));
    }

    /*****
     * 将当前商品的排序加1 顺序下降一位
     * @param mallMaterial
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Boolean> reSortOrderPlus(@RequestBody MallMaterial mallMaterial){
        return HttpResult.ok(mallMaterialService.reSortOrderPlus(mallMaterial));
    }
}
